class_name Weapon
extends CharacterBody2D

#region 属性
var blackboard:Blackboard
var owner_entity:Entity
#endregion
#region 字段
var entity:Entity_Weapon:
	get:
		if not entity:
			entity = Utils.find_child_by_type(self,Entity_Weapon,-1)
		return entity
var weapon_state:GameEnum.WeaponState = GameEnum.WeaponState.Pickable:
	set(value):
		if weapon_state != value:
			var last_weapon_state = weapon_state
			weapon_state = value
			weapon_state_changed.emit(last_weapon_state,weapon_state)
#endregion
#region 信号
signal weapon_state_changed(last_weapon_state:GameEnum.WeaponState,cur_weapon_state:GameEnum.WeaponState)
#endregion

#region 基类方法
#endregion
#region 公共方法
#region 逻辑方法
func init():
	pass
func obtain(entity:Entity):
	owner_entity = entity
	visible = false
	## 需要切换父对象，需要定位到指定父对象槽位
	var slot_componect:SlotComponent = owner_entity.get_component(SlotComponent)
	var slot
	if slot_componect:
		slot = slot_componect.get_slot_manager().get_slot_by_type(GameEnum.SlotType.Weapon)
	self.reparent(owner_entity if not slot else slot)
	self.position = Vector2.ZERO
	if self.scale.y < 0:
		self.rotate(PI)
		self.apply_scale(Vector2(1,-1))
	# TODO 需要调整下初始旋转
	weapon_state = GameEnum.WeaponState.Obtain
	unequip()
func discard():
	## TODO 刚丢弃可能需要一段时间不可拾取，可以在Pickable相关组件里面处理
	## TODO 丢弃动画
	if weapon_state == GameEnum.WeaponState.Equip:
		unequip()
	owner_entity = null
	visible = true
	# 需要切换父对象
	self.reparent(GameWeaponSystem)
	weapon_state = GameEnum.WeaponState.UnPickable
	restore_pickable()
func restore_pickable():
	owner_entity = null
	visible = true
	weapon_state = GameEnum.WeaponState.Pickable
func equip():
	visible = true
	# 绑定Entity按键事件
	var controller_component:ControllerComponent = owner_entity.get_component(ControllerComponent)
	if controller_component:
		controller_component.register_key_action_state_changed_event(on_key_action_state_changed)
	weapon_state = GameEnum.WeaponState.Equip
func unequip():
	visible = false
	# 解绑Entity按键事件
	var controller_component:ControllerComponent = owner_entity.get_component(ControllerComponent)
	if controller_component:
		controller_component.unregister_key_action_state_changed_event(on_key_action_state_changed)
	weapon_state = GameEnum.WeaponState.UnEquip
func destroy():
	weapon_state = GameEnum.WeaponState.Destroy
	queue_free()
#endregion
#region Get方法
func get_weapon_entity() -> Entity:
	return entity
func get_weapon_data() -> EntityData_Weapon:
	if entity:
		return entity.entity_data
	return null
func get_owner_entity() -> Entity:
	return owner_entity
func get_component(component_type:Variant):
	return Utils.find_child_by_type(self,component_type,-1)
func get_sprite_2d() -> Sprite2D:
	return %Sprite2D
func get_animation_player() -> AnimationPlayer:
	return %AnimationPlayer
#endregion
func on_key_action_state_changed(key_action:GameEnum.KeyAction,key_action_state:GameEnum.KeyActionState):
	var wc_state_manager:StateComponent_Weapon = entity.get_component(StateComponent_Weapon)
	if wc_state_manager:
		var state_chart:StateChart = wc_state_manager.state_chart
		if state_chart:
			state_chart.set_expression_property("Controller_" + Utils.get_enum_key_by_value(GameEnum.KeyAction,key_action),key_action_state)
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _ready() -> void:
	init()
#endregion
